package bookstore;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Administrator {

	//ログイン名
	private String loginName;
	//パスワード
	private String password;

	public Administrator(String loginName, String password){
		this.loginName = loginName;
		this.password  = password;
	}

	/**
	 * @return loginName
	 */
	public String getLoginName() {
		return loginName;
	}

	/**
	 * @param loginName セットする loginName
	 */
	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	/**
	 * @return password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password セットする password
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	public boolean login() throws SQLException {
		Connection conn = null;
		try{
			conn = new ResourceFinder().getConnection();
			Statement st = conn.createStatement();
			ResultSet rs = st.executeQuery("SELECT id, password FROM administrator");
			if ( rs.next()){
				if( loginName.equals(rs.getString(1)) &&
				    password.equals(rs.getString(2))) {
				    	return true;
				}
				return false;
			}
			rs.close();
			st.close();
		}finally{
			if ( conn != null ){
				try{
					conn.close();
				}catch (SQLException ignore){}
			}
		}
		return false;
	}
}
